<?php
/**
 *
 *
 */
class SmtpController extends AppController
{
    public $name       = "Smtp";
    public $uses       = array('SmtpSetting');
    public $components = array('MyEmail');


    public function smtpmgnt()
    {
        $this->Authenticate->checkMyLogin(9);
        $this->theme = "admin";

        // process form requests
        if ($this->data['flag']) {
            switch($this->data['flag']){
                case "delete":
                    $this->Authenticate->checkMyLogin(7);
                    if ($this->data['row']['id']) {
                        foreach($this->data['row']['id'] as $v){
                            $this->SmtpSetting->_save(array('deleted' => 1), $v);
                        }
                    }
                    break;

                case "makedefault":
                    $active = $this->SmtpSetting->get_active_smtp();
                    $new = $this->SmtpSetting->_save(array('active' => 1), $this->data['smtpid']);
                    $old = $this->SmtpSetting->_save(array('active' => 0), $active['SmtpSetting']['smtpId']);
                    if ($new || $old) {
                        $alert = "error";
                    } else {
                        $alert = "success";
                    }
                    break;
            } // switch
        }

        $limit = isset($this->data['limit'])?$this->data['limit']:10;
        $this->paginate['conditions'][] = array('deleted' => 0);
        $this->paginate['limit'] = $limit;
        $this->paginate['order'] = array('active' => 'DESC');
        $data = $this->paginate('SmtpSetting');

        self::set('smtps', $data);
        self::set('cnt', count($data));
        self::set('alert', isset($alert)?$alert:"");
    }

    public function smtpadd()
    {
        $this->Authenticate->checkMyLogin(6);
        $this->theme = "admin";

        $data['host'] = $this->data['smtp']['host'];
        $data['uid'] = $this->data['smtp']['uid'];
        $data['pwd'] = $this->data['smtp']['password'];
        $data['incoming'] = $this->data['smtp']['incoming'];
        $data['outgoing'] = $this->data['smtp']['outgoing'];
        $data['timeout'] = $this->data['smtp']['timeout'];
        $data['active'] = 0;
        $data['deleted'] = 0;
        if (self::setter($data)) {
            $alert = "Successfully added a smtp host";
        }

        self::set('alert', isset($alert)?$alert:null);
    }

    public function smtpedit($id = "")
    {
        $this->Authenticate->checkMyLogin(8);
        $this->theme = "admin";

        $data['host'] = $this->data['smtp']['host'];
        $data['uid'] = $this->data['smtp']['uid'];
        $data['pwd'] = $this->data['smtp']['password'];
        $data['incoming'] = $this->data['smtp']['incoming'];
        $data['outgoing'] = $this->data['smtp']['outgoing'];
        $data['timeout'] = $this->data['smtp']['timeout'];
        $data['active'] = $this->data['smtp']['active'];
        $data['deleted'] = 0;
        if (self::setter($data, $id)) {
            $alert = "Successfully updated a smtp host";
        }

        $smtp_info = $this->SmtpSetting->get_smtp(array('smtpId' => $id), 'first');
        self::set('smtp_info', $smtp_info);
        self::set('smtpid', $id);
        self::set('alert', isset($alert)?$alert:null);
    }



    /**
     * protected methods
     */
    protected function setter($data, $id = 0)
    {
        if ($data['host'] && $data['uid'] && $data['pwd'] && $data['outgoing']) {
            return $this->SmtpSetting->_save($data, $id);
        } else {
            return null;
        }
    }
}
?>